<?php
/*
 * Author :pandiam &basanas
 */
session_start();

if (!isset($_SESSION['uid']) || $_SESSION['uid'] == -1) {
    UI_registerError('msg_warning', "You are not logged in.");
    header('Location: login.php');
    exit(0);
}

require_once ('../BL/BL.php');
require_once ('Include/functions.php');

$attributes = array();
$mods = array();
$users = array();

$error_attr = "";
$error_mods = "";
$error_users = "";

if ( isset($_GET['id']) )
{
    if ( !ctype_digit((string)$_GET['id']) ) {
        UI_registerError('msg_error', "Invalid category request");
        header("location: index.php");
        exit(0);
    }
    $catid = $_GET['id'];
}
else
{
    UI_registerError('msg_warning', "You haven't selected a category to edit!");
    header("location: index.php");
    exit(0);
}

createAttributeList();
createModeratorList();
createUserList();

if ( ($retval = BL_editCategory($catid,$_POST['catType'],$attributes, $mods, $users)) < 0) {
    UI_registerError('msg_error', "Could not edit category due to technical error. (Error code: ".$retval.")");
    $_SESSION['error1'] = $_POST['attrs'];
    $_SESSION['error2'] = $_POST['mods'];
    $_SESSION['error3'] = $_POST['users'];
    $_SESSION['error4'] = $_POST['irrelev'];
    header("location: editCategory.php?id=".$catid);
    exit(0);
}
else
{
    UI_registerError('msg_success', "Category edited successfully!");
    header("location: editCategory.php?id=".$catid);
    exit(0);
}

function createAttributeList()
{
    global $attributes, $error_attr, $catid;
    $attrib = split(",", $_POST['attrs']);
    $i = 0;
    $error = "";
    foreach ($attrib as $attribute) {
        $values = split("~", $attribute);
        $attr = new Attribute();
        $attr->setCategoryId($catid);
        $attr->setName($values[0]);
        
        if(strcmp($values[1],"measurable") ==0)
            $attr->setType("m");
        else if(strcmp($values[1],"unmeasurable") ==0)
            $attr->setType("u");
        else
            $attr->setType("n");
        
        if(strcmp($values[1],"measurable") ==0){
            $attr->SetBestValue($values[2]);
    
            $error = $values[0] . "~" . $values[1] . "~" . $values[2];
    
            if (strcmp($values[2], "close_to") == 0) {
                $attr->SetCloseToValue($values[3]);
                $error = $error . "~" . $values[3];
            }
        }
        else{
            $error = $values[0] . "~" . $values[1];
        }
        $attributes[$i] = $attr;
        if ($error_attr == "")
            $error_attr = $error;
        else
            $error_attr = $error_attr . "-" . $error;
        $i++;
        $error = "";
    }
}

function createModeratorList()
{
    global $mods, $error_mods;
    $mods = split(",", $_POST['mods']);
    $i = 0;

}

function createUserList()
{
    global $users, $error_users;
    $users = split(",", $_POST['users']);
    $i = 0;

}

?>